<?php
/*
AceGame. Web browser MMORPG.
Copyright(C) 2012 Aceapps Aplicaciones. 
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses
*/

	// Session Start

	session_start();

	// Load files

	require_once '../include/functions.php';

	// Login & Character Validation

	if (!char_selected()) {
		header('Location: ../users/');
		exit;
	}

	// Get Session Variables

	$charid = $_SESSION['charid'];

	// Battle Redirect

	if (battle_check($charid)) {
		header('Location: battle.php');
		exit;
	}

	// Get Info

	$query = "SELECT c.name,hp,m.name,r.name,c.str,c.int,c.agi,c.stats,m.id,count(cq.id)
		FROM characters AS c,maps AS m,races AS r, charquests AS cq
		WHERE c.id = $charid AND c.map = m.id AND race = r.id AND c.id = cq.charid
		AND complete = TRUE";
	$result = mysql_query($query) or die(mysql_error());
	$row = mysql_fetch_array($result);
	$charname = $row[0];
	$hp = $row[1];
	$mapname = $row[2];
	$race = $row[3];
	$str = $row[4];
	$int = $row[5];
	$agi = $row[6];
	$stats = $row[7];
	$map = $row[8];
	$quests = $row[9];

	// Get Spells

	$query = "SELECT name FROM charspells,spells 
		WHERE charid = $charid AND spell = spells.id";
	$spellr = mysql_query($query) or die(mysql_error());

	// Get Items

	$query = "SELECT name,amount FROM charitems,items 
		WHERE charid = $charid AND item = items.id";
	$itemr = mysql_query($query) or die(mysql_error());
	$numitems = mysql_num_rows($itemr);

	// Get Routes without Requirement

	$query = "SELECT start,name FROM routes,maps WHERE end = $map AND start =maps.id";
	$routes = mysql_query($query) or die(mysql_error());

	// Get Routes with Requirement

	$query = "SELECT end,name,quests FROM routes,maps WHERE start = $map AND end = maps.id";
	$router = mysql_query($query) or die(mysql_error());

	// Get Mobs

	$query = "SELECT * FROM spawns,mobs WHERE map = $map AND mob = mobs.id AND npc = false";
	$mobr = mysql_query($query) or die(mysql_error());
	$nummobs = mysql_num_rows($mobr);

	// Get NPCs

	$query = "SELECT mobs.id,name FROM spawns,mobs WHERE map = $map AND mob = mobs.id AND npc = true";
	$npcr = mysql_query($query) or die(mysql_error());
	$numnpcs = mysql_num_rows($npcr);

	// Fill HTML5 Info

	$tabs = '<li id="tabHeader_1">'.$charname.'</li>
		<li id="tabHeader_2">Spells</li>
		<li id="tabHeader_3">Items</li>
		<li id="tabHeader_4">'.$mapname.'</li>';

	// Character Info

	$content = '<div class="tabpage" id="tabpage_1">
			<p><img name="pic" src="../images/Neutral '.$race.'.png" border="0"></p>
			<p>HP: '.$hp.'/100 <br> STR: '.$str.' <br> INT: '.$int.' <br> AGI: '.$agi.'</p>';

	// Check for Free Stat Points

	if ($stats) $content .= 'You got remaining stats!';
	
	$content .= '</div>';

	// Spells Info

	$content .= '<div class="tabpage" id="tabpage_2">';
	while ($row = mysql_fetch_array($spellr)) $content .= '<p>- '.$row[0].'</p>';
	$content .= '</div>';

	// Inventory

	$content .= '<div class="tabpage" id="tabpage_3">';
	while ($row = mysql_fetch_array($itemr)) $content .= '<p>- '.$row[0].' x'.$row[1].'</p>';
	$content .= '</div>';

	// Map Info

	$content .= '<div class="tabpage" id="tabpage_4">';

	// Show NPCs

	if ($numnpcs) {
		$content .= '<p>NPCs:</p>';
		$i = 0;
		while ($row = mysql_fetch_array($npcr)) {
			$content .= '<p><a href="index.php?npc='.$row[0].'">- '.$row[1].'</a></p>';
		}
	}

	// Show Routes

	$content .= '<p>Routes:</p>';
	while ($row = mysql_fetch_array($routes)) {
		$content .= '<p><a href="cm.php?id='.$row[0].'">- '.$row[1].'</a></p>';
	}
	while ($row = mysql_fetch_array($router)) {
		if ($quests < $row[2]) $content .= '- '.$row[1].' (Missing required quest)';
		else $content .= '<p><a href="cm.php?id='.$row[0].'">- '.$row[1].'</a></p>';
	}

	// Show Mobs

	if ($nummobs) $content .= '<p><a href="pick_enemy.php">There are monsters around!</a></p>';

	$content .= '</div>';

	// Check for NPC

	if (isset($_GET['npc'])) {

		$npc = $_GET['npc'];

		$query = "SELECT mo.name, mo.model FROM spawns AS s,mobs AS mo,maps AS ma
			WHERE mo.id = $npc AND s.mob = mo.id AND s.map = ma.id
			AND ma.id = $map AND npc = true";
		$result = mysql_query($query) or die(mysql_error());

		if (mysql_num_rows($result)) {
			$row = mysql_fetch_array($result);
			$npcname = $row[0];

			$tabs .= '<li id="tabHeader_5">'.$npcname.'</li>';

			// Get NPC Available Quests

			$query = "SELECT id,name FROM quests
				WHERE npc = $npc
				AND id not in (SELECT quest FROM charquests
						WHERE charid = $charid)";
			$questr = mysql_query($query) or die(mysql_error());

			// Get Quests in Progress from NPC

			$query = "SELECT id,name FROM quests
				WHERE npc = $npc
				AND id in (SELECT quest FROM charquests
						WHERE charid = $charid AND complete = false)";
			$qipr = mysql_query($query) or die(mysql_error());

			// Show NPC Info

			$content .= '<div class="tabpage" id="tabpage_5">
				<p><img name="pic" src="../images/mobs/'.$row[1].'.png" border="0"></p>';

			// Show NPC Available Quests

			if (mysql_num_rows($questr) || mysql_num_rows($qipr)) $content .= '<p>Quests:</p>';

			while ($row = mysql_fetch_array($questr))
				$content .= '<p><a href="index.php?npc='.$npc.'&quest='.$row[0].'">'.$row[1].'</a></p>';

			// Show Quests in Progress from NPC

			while ($row = mysql_fetch_array($qipr))
				$content .= '<p><a href="index.php?npc='.$npc.'&quest='.$row[0].'">'.$row[1].'</a> (In Progress)</p>';

			$content .= '</div>';

			if (isset($_GET['quest'])) {

				$quest = $_GET['quest'];

				// Verify Valid Quest

				$query = "SELECT name FROM quests, spawns WHERE quests.id = $quest AND npc = $npc AND npc = mob AND map = $map";
				$result = mysql_query($query) or die (mysql_error());

				// Verify Completed Quest

				$qcq = "SELECT * FROM charquests WHERE charid = $charid AND quest = $quest AND complete = TRUE";
				$qcr = mysql_query($qcq) or die(mysql_error());

				if (mysql_num_rows($result) && !mysql_num_rows($qcr)) {

					$row = mysql_fetch_array($result);

					$tabs .= '<li id="tabHeader_6">'.$row[0].'</li>';

					// Verify Quest In-Progress

					$qipq = "SELECT * FROM charquests WHERE charid = $charid AND quest = $quest AND complete = FALSE";
					$qipr = mysql_query($qipq) or die(mysql_error());

					

					if (mysql_num_rows($qipr)) {

						$content .= '<p>In Progress</p>';

					} else {

						$content .= '<p>Requirements</p>';

					}

				}

			}
		}
	}

	// Load HTML5 Template

	include_once '../template/header.php';

	// Print

	echo '<div class="tabs">
			<ul>
			'.$tabs.'
			</ul>
		</div>

		<div class="tabscontent">
			'.$content.'
		</div>';

	// Load HTML5 Template

	include_once '../template/footer.php';
?>
